using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Modules;
using Ninject.Web.Common;
using SmallJob.Data;
using SmallJob.Data.Interfaces;
using SmallJob.Services.Implements;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Implements;
using SmallShop.Services.Interfaces;

namespace BackEnd
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Paging", // Route name
                "{controller}/{action}/pages/{page}/filter/{filter}", // URL with parameters
                new { controller = "Home", action = "Index", page = UrlParameter.Optional, filter = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();
            BackEnd.Code.AutoUpdate.Start();
            ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            GlobalFilters.Filters.Add(new HandleErrorAttribute());
        }

        protected override IKernel CreateKernel()
        {
            var modules = new INinjectModule[]
            {
                new ServiceModule()
            };

            return new StandardKernel(modules);
        }
    }
    internal class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMConversationService>().To<MConversationService>();
            Bind<IMMessageService>().To<MMessageService>();
            Bind<IMessageService>().To<MessageService>();
            Bind<ITransactionService>().To<TransactionService>();
            Bind<IFollowerService>().To<FollowerService>();
            Bind<IBidRequestService>().To<BidRequestService>();
            Bind<IEmailFormatService>().To<EmailFormatService>();
            Bind<IFeatureService>().To<FeatureService>();
            Bind<IBannerService>().To<BannerService>();
            Bind<ICategoryService>().To<CategoryService>();
            Bind<IRequestService>().To<RequestService>();
            Bind<IProvideService>().To<ProvideService>();
            Bind<IOrderService>().To<OrderService>();
            Bind<IRequestCommentService>().To<RequestCommentService>();
            Bind<IProvideCommentService>().To<ProviceCommentService>();
            Bind<IOfferService>().To<OfferService>();
            Bind<IOfferMessageService>().To<OfferMessageService>();
            Bind<ISkillService>().To<SkillService>();
            Bind<IDatabaseFactory>().To<DatabaseFactory>();
            Bind<IUserService>().To<UserService>();
            Bind<IManagerService>().To<ManagerService>();
            Bind<IDisposable>().To<Disposable>();
        }
    }
}